package com.haoshuang.zhouzhoubang.ui.activity

import android.graphics.Color
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityHandleReturnBinding
import com.haoshuang.zhouzhoubang.ui.adapter.ZbAdapter
import com.haoshuang.zhouzhoubang.ui.vm.HandleReturnViewModel
import com.mufeng.libs.base.BaseActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible


/**
 *  
 */
class HandleReturnActivity : BaseActivity<HandleReturnViewModel, ActivityHandleReturnBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "退款处理"
    }
var  type1=0
var  order_id=0
    override fun initData() {
        order_id=intent.getIntExtra("order_id",0)
        vm.getData2("order_id" to order_id)
        binding.apply {
            adpater = ZbAdapter(list)
            binding.rvList.layoutManager= LinearLayoutManager(this@HandleReturnActivity)
            binding.rvList.adapter=adpater
            tvCommit.clickWithTrigger {

                if (type1==2){
                    val reason=etBty.text.toString()
                    if (reason.isNullOrBlank()){
                        toast("请输入拒绝原因")
                        return@clickWithTrigger
                    }
                    vm.getData("reason" to reason,"order_id" to order_id,"type" to 2)
                }else{
                    vm.getData("order_id" to order_id,"type" to 1)
                }
            }
            tvTy.clickWithTrigger {
                tvTy.setTextColor(Color.parseColor("#ffffff"))
                tvBty.setTextColor(Color.parseColor("#333333"))
                tvTy.setBackgroundResource(R.drawable.bg_4ls)
                tvBty.setBackgroundResource(R.drawable.bg_4f5)
                type1=1
                etBty.gone()
            }
            tvBty.clickWithTrigger {
                tvTy.setTextColor(Color.parseColor("#333333"))
                tvBty.setTextColor(Color.parseColor("#ffffff"))
                tvBty.setBackgroundResource(R.drawable.bg_4ls)
                tvTy.setBackgroundResource(R.drawable.bg_4f5)
                type1=2
                etBty.visible()
            }
        }
    }
    lateinit var adpater: ZbAdapter
    var list = arrayListOf<String>()
    override fun startObserve() {
        vm.getData2.observeState(this) {
                    onLoading { showLoading() }
                    onError { code, msg ->
                        dismissLoading()
                        if (code!="402"){
                            toast(msg)
                        }
                    }
                    onSuccess {
                        dismissLoading()
                        if (it?.is_more==0){
                            binding.tvMoney.text=it?.service_money
                        }else{
                            binding.tvMoney.text=it?.service_money
                            binding.tvMoney.text="${it?.service_money}(退还用户${it?.refund_money})"
                        }

                        list.addAll(it?.content!!)
                        adpater.notifyDataSetChanged()
                    }
                }
        vm.getData.observeState(this) {
                    onLoading { showLoading() }
                    onError { code, msg ->
                        dismissLoading()
                        toast(msg)
                    }
                    onSuccess {
                        dismissLoading()
                        toast("提交成功")
                        finish()
                    }
                }
    }

}